/**
 * ^2_3^(220102) 向后端请求用户的菜单，动态生成路由
 */
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { constantRouterMap } from '@/config/router.config'
import { generatorDynamicRouter } from '@/router/generator-routers'

// console.log('^2_3^......^_^...^_^......^2_3^')

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    // 生成状态路由
    GenerateRoutes ({ commit }, data) {
      return new Promise(resolve => {
        const { token } = data
        console.log('>GenerateRoutes token', token, data)
        generatorDynamicRouter(token).then(routers => {
          if (routers['code'] && routers['code'] !== 0) {
            storage.remove(ACCESS_TOKEN)
          } else {
            commit('SET_ROUTERS', routers)
            console.log('>动态菜单', routers)
            console.log('>permission.state.routers', permission.state.routers)
            resolve()
          }
        })
      })
    }
  }
}

export default permission
